
import scrapy
from flask import current_app
from scrapy import Request
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

from app import evn_
from spiders.items import StockFundsItem
from utils.util import CalculationTool, SpiderTool


class StockFunds1Spider(scrapy.Spider):
    name = 'stock_funds_1'
    stock_list = []

    def start_requests(self):
        '''
        代替start_urls,从数据库读取板块编码，拼接读取股票信息
        :return: 示例：http://data.eastmoney.com/bkzj/BK0537.html
        http://quote.eastmoney.com/sz002240.html
        http://quote.eastmoney.com/concept/sh600031.html#
        '''
        url_2 = 'http://quote.eastmoney.com/concept/%s.html#'

        for stockInfo in self.stock_list:
            stock_code = getattr(stockInfo, 'stock_code')
            market = getattr(stockInfo, 'market')
            if stock_code and market:
                code = market + stock_code
                meta = {'name': getattr(stockInfo, "stock_name"), 'code': stock_code}
                yield Request(url_2 % code, callback=self.parse_concept_item, meta=meta)

    def __init__(self, stock_list):
        self.stock_list = stock_list
        chrome_options = Options()
        chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument('lang=zh_CN.UTF-8')
        chrome_options.add_argument('headless')  # 无头浏览器
        chrome_options.add_argument('--no-sandbox')  # 必要！！
        chrome_options.add_argument('--disable-dev-shm-usage')  # 必要！！
        prefs = {
            "profile.managed_default_content_settings.images": 2,  # 禁止加载图片
            'permissions.default.stylesheet': 2,  # 禁止加载css
        }
        chrome_options.add_experimental_option("prefs", prefs)
        self.bro = webdriver.Chrome(executable_path=evn_.CHROMEDRIVER_PATH, chrome_options=chrome_options)

    def parse_concept_item(self, response):
        current_app.logger.info('{}'.format(response))
        '''
        股票概念信息，筹码集中度+资金数据+财报数据
        :param response:
        :return:
        '''

        stock_code = response.meta["code"]  ##取出数据
        stock_name = response.meta["name"]  ##取出数据

        item = StockFundsItem()
        item['stock_name'] = stock_name
        item['stock_code'] = stock_code
        tradestatus = response.xpath('//*[@id="tradestatus"]/text()').extract_first()
        if ('已收盘' == tradestatus):

            self.bro.get(response.url)

            if SpiderTool.is_visible(self.bro, 'cmfb-btn', by=By.ID):
                self.bro.find_element_by_id('cmfb-btn').click()

            if SpiderTool.is_visible(self.bro, 'chart-container', by=By.ID):
                # 时间  //*[@id="chart-container"]/div[1]/div[3]/div[1]/span
                item['create_time'] = SpiderTool.get_value(self.bro,
                                                           '//*[@id="chart-container"]/div[1]/div[3]/div[1]/span')
                # 获利比例
                item['profit_ratio'] = CalculationTool.cutOutTail(SpiderTool.get_value(self.bro,
                                                                                       '//*[@id="chart-container"]/div[1]/div[3]/div[2]/span'))
                # 平均成本
                item['average_cost'] = SpiderTool.get_value(self.bro,
                                                            '//*[@id="chart-container"]/div[1]/div[3]/div[4]/span')
                # 90%成本
                item['cost_90'] = SpiderTool.get_value(self.bro,
                                                       '//*[@id="chart-container"]/div[1]/div[3]/div[5]/span')

                # 集中度
                item['concentration_90'] = CalculationTool.cutOutTail(SpiderTool.get_value(self.bro,
                                                                                           '//*[@id="chart-container"]/div[1]/div[3]/div[6]/span'))

                # 70%成本
                item['cost_70'] = SpiderTool.get_value(self.bro,
                                                       '//*[@id="chart-container"]/div[1]/div[3]/div[7]/span')

                # 集中度
                item['concentration_70'] = CalculationTool.cutOutTail(SpiderTool.get_value(self.bro,
                                                                                           '//*[@id="chart-container"]/div[1]/div[3]/div[8]/span'))
        else:
            current_app.logger.info("stockcode={},tradestatus:{}".format( stock_name, tradestatus))
        yield item
